//
//  WalletDetailTabBarView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/18.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "WalletDetailTabBarView.h"
//
#import "BCUserWalletDataMenu.h"

@interface WalletDetailTabBarView ()

@property (nonatomic, strong) UISegmentedControl *segment;

/**
 *sepLineLayer
 */
@property (nonatomic, strong) CAShapeLayer *sepLineLayer;

@end

@implementation WalletDetailTabBarView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    self.segment = [[UISegmentedControl alloc] init];
    self.segment.apportionsSegmentWidthsByContent = true;
    [self addSubview:self.segment];
    [self.segment addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self.segment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    //
    UIView *bottomLine = [UIView new];
    bottomLine.backgroundColor = [UIColor separatorColor];
    [self addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(self);
        make.height.mas_equalTo(1.0);
    }];
    //
    self.sepLineLayer = [CAShapeLayer layer];
    self.sepLineLayer.lineWidth = 1;
    self.sepLineLayer.strokeColor = [UIColor separatorColor].CGColor;
    [self.layer addSublayer:self.sepLineLayer];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    //
    self.sepLineLayer.frame = self.bounds;
}


- (void)setMenus:(NSArray<BCUserWalletDataMenu *> *)menus {
    _menus = menus;
    //
    [self.segment removeAllSegments];
    NSInteger count = menus.count;
    for (int i=0; i<count; i++) {
        BCUserWalletDataMenu *menu = menus[i];
        [self.segment insertSegmentWithTitle:menu.title atIndex:i animated:false];
    }
    //
    [self setSegementLines];
    //
    if (count) {
        self.segment.selectedSegmentIndex = 0;
        [self.segment sendActionsForControlEvents:UIControlEventValueChanged];
    }
    //
    self.segment.tintColor = [UIColor whiteColor];
    [self.segment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor qmui_colorWithHexString:@"#333333"]} forState:UIControlStateNormal];
    [self.segment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor darkMainColor]} forState:UIControlStateSelected];
}
/**
 设置分隔线
 */
- (void)setSegementLines {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSInteger segmentCount = self.segment.numberOfSegments;
        if (segmentCount>1) {
            CGFloat width = CGRectGetWidth(self.frame);
            CGFloat height = CGRectGetHeight(self.frame);
            CGFloat lineHeight = height/3;
            //
            CGFloat startY = height/2-lineHeight/2;
            CGFloat endY = height/2+lineHeight/2;
            //
            UIBezierPath *sepPath = [UIBezierPath bezierPath];
            //获取UISegment
            NSArray *subviews = self.segment.subviews;
            for (UIView *segment in subviews) {
                if ([segment isKindOfClass:NSClassFromString(@"UISegment")]) {
                    CGFloat x = CGRectGetMaxX(segment.frame);
                    if (x<width) {
                        [sepPath moveToPoint:CGPointMake(x, startY)];
                        [sepPath addLineToPoint:CGPointMake(x, endY)];
                    }
                }
            }
            self.sepLineLayer.path = sepPath.CGPath;
        }else {
            self.sepLineLayer.path = nil;
        }
    });
}




- (void)segmentValueChanged:(UISegmentedControl *)segment {
    if (self.delegate && [self.delegate respondsToSelector:@selector(walletDetailTabBarView:valueChanged:atIndex:)]) {
        NSInteger index = segment.selectedSegmentIndex;
        [self.delegate walletDetailTabBarView:self valueChanged:self.menus[index] atIndex:index];
    }
}


+ (CGFloat)defaultHeight {
    return 44.0;
}

@end
